import { getUID } from '@/utils';
const reqList: string[] = [];
const addReqList = (id: string) => {
  reqList.push(id);
};
const removeReqList = (config: any) => {
  const { reqId ='' } = config;
  const index = reqList.indexOf(reqId);
  reqList.splice(index, 1);
};
const reasonConfigHandleLoading = (config:any, type:number) => {
  // config: 请求配置; type: 1添加 2移出
  const { mergeConfig } = config;
  if (!mergeConfig || !mergeConfig.noLoading) {
    if (type === 1) {
      const id = getUID();
      config.reqId = id;
      addReqList(id);
    } else {
      removeReqList(config.reqId);
    }
  }
};
const crmFormatHandler = {
  request: {
    onFulfilled: (config: any) => {
      if (config.version) {
        config.headers['version_code'] = config.version;
      }
      config.headers['Content-Type'] = config.contentType || 'application/json';
      // 增加请求id
      reasonConfigHandleLoading(config, 1);
      return config;
    }
  }
};

export default crmFormatHandler;
